import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * @author zyj
 * @date 2022年05月23日 14:37
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Student> set = new TreeSet<Student>(new MyComparator());
        System.out.println(set.add(new Student("1", "张三", "1")));
        System.out.println(set.add(new Student("2", "李四", "2")));
        System.out.println(set.add(new Student("3", "王五", "3")));
        System.out.println(set.add(new Student("3", "张无忌", "4")));
        for (Student s: set) {
            System.out.println(s);
        }

        TreeSet<Student> map = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getId().compareTo(o2.getId()) > 0){
                    return 1;
                }else if (o1.getId().compareTo(o2.getId()) < 0){
                    return -1;
                }else {
                    if (o1.getName().compareTo(o2.getName()) > 0) {
                        return -1;
                    } else if (o1.getName().compareTo(o2.getName()) < 0) {
                        return 1;
                    }
                    return 0;
                }
            }
        });


        TreeSet<Student> map1 = new TreeSet<Student>((o1,o2) ->{
            if (o1.getId().compareTo(o2.getId()) > 0){
                return 1;
            }else if (o1.getId().compareTo(o2.getId()) < 0){
                return -1;
            }else {
                if (o1.getName().compareTo(o2.getName()) > 0) {
                    return -1;
                } else if (o1.getName().compareTo(o2.getName()) < 0) {
                    return 1;
                }
                return 0;
            }
        }
        );

        System.out.println("******************************");
        TreeSet<Student> students = new TreeSet<>();

        System.out.println(students.add(new Student("1", "张三", "1")));
        System.out.println(students.add(new Student("2", "李四", "2")));
        System.out.println(students.add(new Student("3", "王五", "3")));
        System.out.println(students.add(new Student("3", "张无忌", "4")));
        for (Student s: students) {
            System.out.println(s);
        }

    }
}

class MyComparator implements Comparator<Student>{


    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getId().compareTo(o2.getId()) > 0){
            return 1;
        }else if (o1.getId().compareTo(o2.getId()) < 0){
            return -1;
        }else {
            if (o1.getName().compareTo(o2.getName()) > 0) {
                return -1;
            } else if (o1.getName().compareTo(o2.getName()) < 0) {
                return 1;
            }
            return 0;
        }
    }
}